package net.neiquan.neiquandemowithkf.fragment;


import android.view.View;


import net.neiquan.neiquandemowithkf.adapter.GoodAdapter;
import net.neiquan.neiquandemowithkf.api.NetUtils;
import net.neiquan.neiquandemowithkf.entity.Good;
import net.neiquanlib.base.BaseRecycleViewFragment;
import net.neiquanlib.base.MyBaseAdapterRecycleView;
import net.neiquanlib.http.NetCallBack;
import net.neiquanlib.http.ResultModel;

import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.ToastUtil;

import java.util.List;

/**
 * 作者 : hjb
 * 时间 : 2016/6/22.
 */
public class MyListFragment extends BaseRecycleViewFragment {


    @Override
    protected boolean isHaveHead() {
        return false;
    }



    @Override
    protected MyBaseAdapterRecycleView getAdapter() {
        return new GoodAdapter(mContext,null);
    }

    @Override
    protected int getLineNum() {
        return 1;
    }
    @Override
    protected void setHeadViews() {
        adapter.setOnItemClickListener(new MyBaseAdapterRecycleView.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, List data, int position) {
                AppLog.e("-----------" + position);
                Good good = (Good) data.get(position);
                AppLog.e("------good-----" + good.toString());
            }
        });
    }

    @Override
    protected void getDateFromWeb(final boolean isRefush) {
        NetUtils.getInstance().hotCommodity(new NetCallBack() {
            @Override
            public void onSuccess(String message, int errorCode, String response, ResultModel model) {
                List<Good> commodities = (List<Good>) model.getModelList();
                mRecyclerView.refreshComplete();
                mRefreshLayout.hideAll();
                //刷新
                if (isRefush) {   //刷新
                    adapter.clear();  //清空
                    if ((commodities == null) || (commodities.size() == 0)) {
                        mRecyclerView.setLoadingMoreEnabled(false);//没有加载更多
                    } else {
                        adapter.append(commodities); //添加
                        mRecyclerView.setLoadingMoreEnabled(false);//没有加载更多
                    }
                } else {
                    mRecyclerView.refreshComplete();
                    mRefreshLayout.hideAll();
                    adapter.append(commodities); //添加
                    mRecyclerView.setLoadingMoreEnabled(false);//没有加载更多
                }
            }

            @Override
            public void onFail(String message, int errorCode, boolean dataError) {
                mRecyclerView.refreshComplete();
                mRefreshLayout.hideAll();
                if (message != null) {
                    ToastUtil.shortShowToast(message);
                }
            }
        }, Good.class);
    }

    @Override
    public void onRetryClick() {
        getDateFromWeb(true);
    }

    @Override
    public void onRefresh() {
        getDateFromWeb(true);
    }

    @Override
    public void onLoadMore() {
        getDateFromWeb(false);
    }
}
